﻿/*********************************************************
 * copyright learnren.com 版权所有
 * 开发人员：minyuan
 * 创建时间：2013/6/27 23:22:02
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;
using System.IO;
using LEARNREN.COM.TOOL;
using LEARNREN.COM.DAL;

namespace LEARNREN.COM.BLL
{
    public class BannerLogic : LogicBase<Banner>
    {
        public BannerLogic()
        {

        }
        public BannerLogic(DB4ODALClient client)
            : base(client)
        {

        }

        /// <summary>
        /// 更新幻灯片Js
        /// </summary>
        /// <param name="catalogueId">幻灯片所属栏目Id，默认为0,0表示所有</param>
        public void UpdateBannerJs(long catalogueId = 0)
        {
            try
            {
                string template = FilePathHelpercs.CommonJsTemplate;
                if (File.Exists(template))
                {
                    string jsContent = File.ReadAllText(template);
                    var banners = GetList();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < banners.Count; i++)
                    {
                        sb.AppendLine(string.Format("img[{0}] = '{1}';", i, banners[i].Image.TrimStart('~')));
                        sb.AppendLine(string.Format("lnk[{0}] = escape('{1}');", i, banners[i].Link));
                        sb.AppendLine(string.Format("txt[{0}] = '{1}';", i, banners[i].Title));
                    }
                    string real = jsContent.Replace("{BinnerList}", sb.ToString());
                    File.WriteAllText(FilePathHelpercs.CommonJs, real);
                }
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
            }
        }

        public Banner GetById(long id)
        {
            return GetList(p => p.Id == id).FirstOrDefault();
        }
    }
}
